<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>我是一阵风直播</title>
    <link rel="stylesheet" type="text/css" href="../css/aui.css"/>
</head>
<body>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript" src="../script/aui-toast.js"></script>

<script type="text/javascript">
    var alivcLivePusher = null;
    var pushRUL = '';
    var resolution = 720;
    var direction = 0;
    var fsPath = '';
    apiready = function() {
        var viewParm = api.pageParam;
        if(viewParm.rtmp_url != ''){pushRUL = viewParm.rtmp_url;}
        if(viewParm.resolution > 100){resolution = viewParm.resolution;}
        if(viewParm.direction != 0){direction = 1;}
        alivcLivePusher = api.require('alivcLivePusher');

        initPusherL();
        alivcLivePusher.setpreviewDisplayMode( //设置阿里云推流全屏
              {
              displayModel:2,
              },function(ret){
                  alert('全屏');
              });

        setTimeout(function() {
            // 打开菜单页面
            openFrm('video_push_info'+direction+'_frm');
        }, 1000);
        //监听推流相关事件
        alivcLivePusher.addEventListener(function(ret, err){
            eventListenerL(ret.eventType);
        });
        //监听返回
        api.addEventListener({name:'menuGoBack'},function(ret, err){
            goBack();
        });
        //监听暂停开始
        api.addEventListener({name:'stopAndStart'},function(ret, err){
            var type = ret.value.type;
            if(type == 'pauseL'){
                pauseL();
            }else if(type == 'resumeL'){
                resumeL();
            }
        });
        //监听是否静音
        api.addEventListener({name:'setMuteL'},function(ret, err){
            setMuteL(ret.value.type);
        });
        //监听切换摄像头
        api.addEventListener({name:'switchCameraL'},function(ret, err){
            switchCameraL();
        });
        //监听设置闪光灯
        api.addEventListener({name:'setFlashL'},function(ret, err){
            setFlashL(ret.value.type);
        });
        //监控开始录屏
        api.addEventListener({name:'startScreenCaptureL'},function(ret, err){
            startScreenCaptureL();
        });
        //监控停止录屏
        api.addEventListener({name:'stopScreenCaptureL'},function(ret, err){
            stopScreenCaptureL();
        });
        //监控打开相机预览
        api.addEventListener({name:'startCameraL'},function(ret, err){
            startCameraL();
        });
        //监控打开美颜
        api.addEventListener({name:'setBeautyOn'},function(ret, err){
            setBeautyOnL(ret.value.type);
        });

    };
    //初始化推流
    function initPusherL() {
        alivcLivePusher.initPusher({
            // resolution: resolution,
            // initialVideoBitrate: 800,
            // targetVideoBitrate: 800,
            // minVideoBitrate: 400,
            // qualityMode: 'ResolutionFirst',
            // beautyMode: 'professional',
            // previewOrientation: 'PORTRAIT',
            // externMainStream: true,
            // fps: 20,
            // videoOnly: false,
            // audioOnly:false,
            // cameraType: 'front'
            resolution:'720P',   //分辨率
    				initialVideoBitrate:1200,  //初始化码率
    				targetVideoBitrate:400,   //目标码率
    				minVideoBitrate:100,      //最小码率
    				beautyEnable:false,       //是否开启美颜
    				previewOrientation:'PORTRAIT',  //推流方向设置
    				fps:20,     //帧率
    				waterMarkImage:
    				 {
    					 //path:'fs://shuiyin.png',
    					 path:'widget://image/shuiyin.png',               //字符串类型，图片的路径，支持fs://,widget://
    					 coordX:0.1,            //数字类型，水印左上顶点x的相对坐标，默认0.1 取值范围：[0,1]
    					 coordY:0.1,            //数字类型，水印左上顶点y的相对坐标，默认0.1 取值范围：[0,1]
    					 width:0.3,               //数字类型，水印的相对宽度 (水印会根据水印图片实际大小和水印宽度等比缩放)，默认0.3 取值范围：[0,1]
    				 }
        }, function(ret) {
            if(ret.status){
                console.log(JSON.stringify(ret) + '----->初始化成功');
                setTimeout('startPreviewL()',1000);
            }else{
                console.log('初始化失败');
            }
        })
    }
    //预览
    function startPreviewL() {
        alivcLivePusher.startPreview({
            rect: {
                x: 0, //（可选项）数字类型；模块左上角的 x 坐标（相对于所属的 Window 或 Frame）；默认值：0
                y: 0, //（可选项）数字类型；模块左上角的 y 坐标（相对于所属的 Window 或 Frame）；默认值：0
                w: api.frameWidth, //(可选项)数字类型;模块宽度（相对于所属的 Window 或 Frame;默认300
                h: api.frameHeight//(可选项)数字类型;模块高度（相对于所属的 Window 或 Frame;默认300
            },
            fixedOn: api.frameName,
            fixed: true
        }, function(ret) {
            if(ret.status){
                console.log(JSON.stringify(ret) + '----->预览成功');
                setTimeout('startPushL()',10000);
            }else{
                console.log('预览失败');
            }
        })
    }
    //开始推流
    function startPushL() {
      alivcLivePusher.startPush({
        url:pushRUL
      },function(ret){
          console.log(JSON.stringify(ret) + '----->推流成功');
      })
    }
    //停止推流
    function stopPushL() {
      alivcLivePusher.stopPush();
    }
    //暂停推流
    function pauseL() {
      alivcLivePusher.pause();
    }
    //恢复推流
    function resumeL() {
      alivcLivePusher.resume();
    }
    //重新开始推流
    function restartPushL() {
      alivcLivePusher.restartPush();
    }
    //推流重连
    function reconnectPushL() {
      alivcLivePusher.reconnectPush();
    }
    //销毁推流
    function destroyL() {
      alivcLivePusher.destroy();
    }
    //设置静音
    function setMuteL(flag=0) {
        if(flag == 1){
            alivcLivePusher.setMute({
                isMute:false
            });
        }else{
            alivcLivePusher.setMute({
                isMute:true
            });
        }
    }
    //切换摄像头
    function switchCameraL() {
        alivcLivePusher.switchCamera();
    }
    //设置横屏
    function startPreview() {

    }

    //设置闪光灯
    function setFlashL(type=0) {
        if(type == 0){
            alivcLivePusher.setFlash({
                isFlash:true
            });
        }else{
            alivcLivePusher.setFlash({
                isFlash:false
            });
        }
    }
    //镜像预览
    function setPreviewMirrorL() {
      alivcLivePusher.setPreviewMirror({
          isMirror:true
      });
    }

    //打开美颜
    function setBeautyOnL(type=0) {
        if(type == 0){
            alivcLivePusher.setBeautyOn({
                isBeautyOn:true
            });
        }else{
            alivcLivePusher.setBeautyOn({
                isBeautyOn:false
            });
        }

    }
    //录屏
    function startScreenCaptureL() {
      alivcLivePusher.startScreenCapture({
          url:pushRUL,
          networkPoorImage:fsPath,
          pausePushImage:fsPath
      },function(ret){
          console.log(JSON.stringify(ret));
          startCameraL();
      });
    }
    function stopScreenCaptureL() {
      alivcLivePusher.stopScreenCapture(function(ret){
          console.log(JSON.stringify(ret));
      });
    }
    function startCameraL() {
      alivcLivePusher.startCamera({
          rect:{x:0,y:0,w:300,h:300}
      },function(ret){
          console.log(JSON.stringify(ret));
      });
    }
    function getFsPath() {
      api.getPicture({
          sourceType: 'library',
          encodingType: 'jpg',
          mediaValue: 'pic',
          destinationType: 'url',
          allowEdit: true,
          quality: 50,
          targetWidth: 100,
          targetHeight: 100,
          saveToPhotoAlbum: false
      }, function(ret, err){
          if(ret){
            fsPath = ret.data
            console.log(fsPath);
          }else{
               console.log(JSON.stringify(err));
          }
      });
    }
    function eventListenerL(eventType){
        var toast = new auiToast();
        if(eventType == 'previewStarted'){
            toast.success({
                title:"预览开始",
                duration:2000
            });
        }else if(eventType == 'previewStoped'){
            toast.success({
                title:"预览停止",
                duration:2000
            });
        }else if(eventType == 'pushStarted'){
            toast.success({
                title:"开始推流",
                duration:2000
            });
        }else if(eventType == 'pushPauesed'){
            toast.success({
                title:"暂停推流",
                duration:2000
            });
        }else if(eventType == 'pushResumed'){
            toast.success({
                title:"恢复推流",
                duration:2000
            });
        }else if(eventType == 'pushStoped'){
            toast.success({
                title:"推送停止",
                duration:2000
            });
        }else if(eventType == 'pushRestarted'){
            toast.success({
                title:"重新推送",
                duration:2000
            });
        }else if(eventType == 'firstFramePreviewed'){
            toast.success({
                title:"首帧预览",
                duration:2000
            });
        }else if(eventType == 'dropFrame'){
            toast.success({
                title:"丢帧",
                duration:2000
            });
        }else if(eventType == 'adjustBitRate'){
            toast.success({
                title:"码率调整",
                duration:2000
            });
        }else if(eventType == 'adjustFps'){
            toast.success({
                title:"帧率调整",
                duration:2000
            });
        }else if(eventType == 'networkPoor'){
            toast.success({
                title:"网络差",
                duration:2000
            });
        }else if(eventType == 'networkRecovery'){
            toast.success({
                title:"网络恢复",
                duration:2000
            });
        }else if(eventType == 'reconnectStart'){
            toast.success({
                title:"重连开始",
                duration:2000
            });
        }else if(eventType == 'reconnectFail'){
            toast.success({
                title:"重连失败",
                duration:2000
            });
        }else if(eventType == 'reconnectSucceed'){
            toast.success({
                title:"重连成功",
                duration:2000
            });
        }else if(eventType == 'sendDataTimeout'){
            toast.success({
                title:"发送数据失败",
                duration:2000
            });
        }else if(eventType == 'connectFail'){
            toast.success({
                title:"连接失败",
                duration:2000
            });
        }else if(eventType == 'systemError'){
            toast.success({
                title:"系统错误",
                duration:2000
            });
        }


    }
</script>
</html>
